#include <QtGui>
#include <iostream>

#include "opendialog.h"
#include "linearCombDialog.h"
#include "../Watermark/linearCombination.h"


OpenDialog::OpenDialog() {
    algorithm1 = NULL;
    algorithm2 = NULL;
    algorithm3 = NULL;
    algorithm4 = NULL;

    img1 = new QLabel;
    img1->setBackgroundRole(QPalette::Base);
    img1->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    
    img2 = new QLabel;
    img2->setBackgroundRole(QPalette::Base);
    img2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);


    open1 = new QPushButton("Select Image", this);
    open2 = new QPushButton("Select Image", this);

    connect(open1, SIGNAL(clicked()), this, SLOT(openIMG1()));
    connect(open2, SIGNAL(clicked()), this, SLOT(openIMG2()));
   
    QGridLayout *images = new QGridLayout;
    images->addWidget(img1, 0, 0);
    images->addWidget(img2, 0, 1);
    images->addWidget(open1, 1, 0);
    images->addWidget(open2, 1, 1);
    
    QComboBox *watermarkTypes = new QComboBox(this);
    watermarkTypes->addItem("Robust");
    watermarkTypes->addItem("Fragile");
    watermarkTypes->addItem("Semi-Fragile");
    watermarkTypes->setCurrentIndex(-1);    
    connect(watermarkTypes, SIGNAL(activated(int)), this, SLOT(algorithms(int)));
   
    QLabel *wmTypesLabel = new QLabel("Select the type of watermark to apply: ");
    
    watermarkAlgorithms = new QVBoxLayout;
    watermarkAlgorithms->addWidget(wmTypesLabel);
    watermarkAlgorithms->addWidget(watermarkTypes);
    
    apply = new QPushButton("Apply", this);
    cancel = new QPushButton("Cancel", this);

    connect(apply, SIGNAL(clicked()), this, SLOT(applyWM()));
    connect(cancel, SIGNAL(clicked()), this, SLOT(close()));
    
    QVBoxLayout *buttons = new QVBoxLayout;
    buttons->addWidget(apply);
    buttons->addWidget(cancel);
    
    QHBoxLayout *algorithmSelection = new QHBoxLayout;
    algorithmSelection->addLayout(watermarkAlgorithms);
    algorithmSelection->addLayout(buttons);

    mainLayout = new QVBoxLayout;
    mainLayout->addLayout(images);
    mainLayout->addLayout(algorithmSelection);

    setLayout(mainLayout);

    setWindowTitle("Select Option for Water Marking");
    resize(450, 350);
}

void OpenDialog::openIMG1() {
    openImage(1, img1);
}

void OpenDialog::openIMG2() {
    openImage(2, img2);
}

void OpenDialog::algorithms(int waterMarkType) {
    
    if (algorithm1) 
        delete algorithm1;
    if (algorithm2)
        delete algorithm2;
    if (algorithm3)
        delete algorithm3;
    if (algorithm4)
        delete algorithm4;

    QString alg1;
    QString alg2;
    QString alg3;
    QString alg4;
        
    switch (waterMarkType) {
        case 0: 
            alg1 = "Robust Algorithm 1";
            alg2 = "Robust Algorithm 2";
            alg3 = "Robust Algorithm 3";
            alg4 = "Robust Algorithm 4";
            break;
        case 1:
            alg1 = "Linear Combination";
            alg2 = "Fragile Algorithm 2";
            alg3 = "Fragile Algorithm 3";
            alg4 = "Fragile Algorithm 4";
            //connect(algorithm1, SIGNAL(clicked()), this, SLOT(linearComb()));
            break;
        case 2:
            alg1 = "Semi-Fragile Algorithm 1";
            alg2 = "Semi-Fragile Algorithm 2";
            alg3 = "Semi-Fragile Algorithm 3";
            alg4 = "Semi-Fragile Algorithm 4";
            break;
    }
    algorithm1 = new QRadioButton(alg1, this);
    algorithm2 = new QRadioButton(alg2, this);
    algorithm3 = new QRadioButton(alg3, this);
    algorithm4 = new QRadioButton(alg4, this);
    
    switch(waterMarkType) {
        case 0:
            break;
        case 1:
            connect(algorithm1, SIGNAL(clicked()), this, SLOT(linearComb()));
            break;
        case 2:
            break;
    }

    watermarkAlgorithms->addWidget(algorithm1); 
    watermarkAlgorithms->addWidget(algorithm2);
    watermarkAlgorithms->addWidget(algorithm3);
    watermarkAlgorithms->addWidget(algorithm4);
}

void OpenDialog::openImage(int imgID, QLabel *imageLabel) {
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select Image"), QDir::currentPath());
    if(!fileName.isEmpty())
    {
        //Pequena gambiarra devido a alguns problemas com ponteiro
        if (imgID == 1)
            imgData1 = new QImage(fileName);
        if (imgID == 2)
            imgData2 = new QImage(fileName);
        QImage image = QImage(fileName).scaled(200, 150, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        if(image.isNull())
        {
            QMessageBox::information(this, tr("Error"), tr("Couldn't open selected file: %1.").arg(fileName));
            return;
        }

        imageLabel->setPixmap(QPixmap::fromImage(image));
    }
}

void OpenDialog::linearComb() {
    lcDialog = new LinearCombDialog;
    connect(lcDialog, SIGNAL(sendAlpha(float)), this, SLOT(setLinearComb(float)));
    lcDialog->exec();
}

void OpenDialog::setLinearComb(float alpha) {
    watermark = new LinearCombination(alpha, imgData1, imgData2);
}

void OpenDialog::applyWM() {
    QImage *finalImg = watermark->applyWatermark();
    emit sendImage(finalImg);
    close();
}
